package com.dgl.sdk.view.advert;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.dgl.sdk.R;
import com.dgl.sdk.view.LoadImage;
import viewpagerindicator.CirclePageIndicator;

import java.util.ArrayList;

/**
 * @Author colin.ding@vipshop.com
 * @ProName VipShopPurchase
 * @Data 2013-11-6
 * 
 * @Comment: 广告
 */
public class Advert {


//	private final static String PUR_ADVERT_ZONE_ID = "88"; //正式环境 72  测试环境81

	private Context context;

	private View mAdvView;
	private View adv_layout;
	private MyViewPager myViewPager;
	private CirclePageIndicator indicator;

	private int advCurrentPosition = 0;//广告当前选中项
	private boolean Is_First = true;//是否第一次初始化广告
	private final int ADV_TAG = 1;
	private final long ADV_TIME = 4000l;//4秒切换一次

	private ArrayList<String> advList;

	private ArrayList<View> views;
 	
	public Advert(Context context, ArrayList<String> urlList) {

		this.context = context;
        advList = urlList;
 	}


	public View getView() {
        initView();
        fillView();

		return mAdvView;
	}




	private void initView() {
		LayoutInflater inflater = LayoutInflater.from(context);

		mAdvView = (LinearLayout) inflater
				.inflate(R.layout.adv_viewpager,null);

		adv_layout = mAdvView.findViewById(R.id.adv_layout);
		myViewPager = (MyViewPager) mAdvView.findViewById(R.id.adViewPager);
		indicator = (CirclePageIndicator) mAdvView.findViewById(R.id.indicator);
	}

	private void fillView() {
		if(advList.size() == 0){
			return;
		}
		
		adv_layout.setVisibility(View.VISIBLE);
	


		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

		views = new ArrayList<View>();
		
		for (int i=0; i< advList.size(); i++) {
			final ImageView image = new ImageView(context);
			image.setScaleType(ScaleType.CENTER_CROP);
			image.setLayoutParams(params);
			image.setOnClickListener(advertClickListener);

			views.add(image);

            LoadImage.display(image, advList.get(i));

		}

		myViewPager.setAdapter(new MyPagerAdapter());
		indicator.setViewPager(myViewPager);
		indicator.setSnap(true);
		indicator.setOnPageChangeListener(changeListener);
		myViewPager.setCurrentItem(0);

		if (myViewPager.getCurrentItem() >= 0) {
			advCurrentPosition = myViewPager.getCurrentItem();
		}
		if (Is_First) {
			goNext();
			Is_First = false;
		}
	}

	public class MyPagerAdapter extends PagerAdapter {
		
		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView((View) arg2);
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public Object instantiateItem(View arg0, int arg1) {
			((ViewPager) arg0).addView(views.get(arg1));
			return views.get(arg1);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}

	private OnClickListener advertClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
	        //
		}
	};

	private OnPageChangeListener changeListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			advCurrentPosition = arg0;
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			switch (arg0) {
			case 0:
				goNext();
				break;
			case 1:
				stopToNext();
				break;
			case 2:
				stopToNext();
				break;
			default:
				break;
			}
		}
	};

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
				if (advList != null && advList.size() > 1 && mAdvView.isShown()) {// 有广告、大于一张、同时显示的时候才执行
					advCurrentPosition++;
					if (advCurrentPosition != myViewPager.getCurrentItem()) {
						int num = advCurrentPosition % advList.size();
						if (num == 0) {
							myViewPager.setCurrentItem(num, false);
						} else {
							myViewPager.setCurrentItem(num, true);
						}
						stopToNext();// 防止切换多次
					}
					goNext();
				}
		}
	};

	private void goNext() {
		handler.sendEmptyMessageDelayed(ADV_TAG, ADV_TIME);
	}

	private void stopToNext() {
		handler.removeMessages(ADV_TAG);
	}

}
